package com.quxuecx.springcloud.controller;

import com.quxuecx.springcloud.entities.CommentResult;
import com.quxuecx.springcloud.entities.Payment;
import com.quxuecx.springcloud.ld.ILoadBalancer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: shaofeer
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: shaofeer@163.com
 * <p>
 * @time: 2020/6/17
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class Order80Controller {
    /**
     * 服务地址
     */
    private static final String PAYMENT_SERVICE_URL = "http://CLOUD-PROVIDER-PAYMENT/payment";

    @Autowired
    RestTemplate mRestTemplate;

    //@Autowired
    //ILoadBalancer mILoadBalancer;
    //@Autowired
    //DiscoveryClient mDiscoveryClient;

    /**
     * 保存数据
     *
     * @param payment
     * @return
     */
    @GetMapping("/save")
    public CommentResult save(Payment payment) {
        CommentResult commentResult = mRestTemplate.postForObject(PAYMENT_SERVICE_URL + "/save", payment, CommentResult.class);

        return commentResult;
    }

    /**
     * 查询数据
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommentResult<Payment> getById(@PathVariable("id") Integer id) {
        CommentResult<Payment> commentResult = mRestTemplate.getForObject(PAYMENT_SERVICE_URL + "/get/" + id, CommentResult.class);

        log.info("查询结果：{}", commentResult.getData());

        return commentResult;
    }

    //@GetMapping("/get/port")
    //public String getPort() {
    //    List<ServiceInstance> services = mDiscoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
    //
    //    ServiceInstance serviceInstance = mILoadBalancer.getServiceInstance(services);
    //
    //    String port = mRestTemplate.getForObject(serviceInstance.getUri() + "/payment/port", String.class);
    //    return port;
    //}
}
